package com.example.a79273.shandianban.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.model.YouHuoProducBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.LinkedList;

/**
 * Created by lenovo on 2016/8/30.
 */
public class ParamsFragment extends Fragment {
    private View view;
    private WebView mWebView;
    private String mPath;
    private String mWebViewPath;
    private RequestParams mParams;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_params,null);
        initView();
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {

    }

    private void setData() {
        //设置WebView
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.setWebChromeClient(new WebChromeClient());

    }

    private void initData() {
        mPath = getArguments().getString("path");
        mParams = new RequestParams(mPath);
        //获取RollPagerView里的图片的地址
        x.http().get(mParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if(!TextUtils.isEmpty(result)) {
                    Type listType = new TypeToken<LinkedList<YouHuoProducBean>>(){}.getType();
                    Gson gson = new Gson();
                    LinkedList<YouHuoProducBean> resources = gson.fromJson(result, listType);
                    YouHuoProducBean youHuoProducBean = resources.get(0);
                    mWebViewPath = youHuoProducBean.getData().getMallDetails().getContent();
                    mWebView.loadDataWithBaseURL(null,mWebViewPath,"text/html","UTF-8",null);
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(getActivity(), "网络出小差啦...", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void initView() {
        mWebView = (WebView) view.findViewById(R.id.wv_params_webview);
    }
}
